package com.mijie.homi.admin.web.reports;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mijie.core.Paging;
import com.mijie.homi.admin.domain.commom.CrashStat;
import com.mijie.homi.admin.service.message.MessageService;
import com.mijie.homi.admin.service.notify.NotifyUserAgent;
import com.mijie.homi.admin.service.reports.ReportService;
import com.mijie.homi.common.constant.TypeCode;
import com.mijie.homi.common.domain.SystemMessage;
import com.mijie.homi.common.domain.reports.Crash;
import com.mijie.homi.common.domain.reports.Feedback;
import com.mijie.homi.common.protocol.HomiProtocol;

@Controller
public class ReportController {

	@Autowired
	private ReportService service;
	@Autowired
	private MessageService messageService;
	@Autowired
	private NotifyUserAgent notifyUserAgent;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/crash/list")
	public String list(Paging<Crash> paging,Model model,HttpServletRequest request){
		Map<String,Object> searchBundle = (Map<String, Object>) request.getAttribute("searchBundle");
		service.getCrashPage(paging, searchBundle);
		List<Crash> list = paging.getList();
		for(Crash c : list){
			c.setDeviceInfo(c.getDeviceInfo().replaceAll("\\|", "<br />"));
			c.setErrorInfo(c.getErrorInfo().replaceAll("\r\n|\n|\r", "<br />"));
		}
		model.addAllAttributes(searchBundle);
		return "crash/list";
	}
	
	@RequestMapping(value="/crash/changeStatus")
	public String changeCrashStatus(String errorIdentifier,int currentStatus) 
			throws IOException{
		if(currentStatus>1) currentStatus = 1;
		if(currentStatus<0) currentStatus = 0;
		service.changeCrashStatus(errorIdentifier, currentStatus ^ 1);
		return "redirect:/crash/list.shtml";
	}
	
	
	@RequestMapping(value="/feedback/delete")
	public String deleteFeedback(int[] ids) 
			throws IOException{
		service.deleteFeedback(ids);
		return "redirect:/feedback/list.shtml";
	}
	
	@RequestMapping(value="/feedback/list")
	public String list(Paging<Feedback> paging,Model model,
			HttpServletRequest request,HttpServletResponse response) 
			throws IOException{
		service.getFeedbackPage(paging);
		model.addAttribute("paging", paging);
		return "feedback/list";
	}
	
	@RequestMapping("/feedback/reply/create")
	public String  feedbackReplyCreate(int userId,int feedbackId,Model model) throws IOException {
		model.addAttribute("userId", userId);
		model.addAttribute("feedbackId", feedbackId);
		return "feedback/reply";
	}
	
	@RequestMapping("/feedback/reply")
	public void  feedbackReply(int userId,int feedbackId,SystemMessage msg,HttpServletResponse response) throws IOException {
		msg.setCreateAt(new Date());
		msg.setLink("");
		msg.setLinkName("");
//		int id = messageService.createSystemMessage(msg);
//		if(id>0){
			notifyUserAgent.sendSystemMessage(userId, msg);
			service.markFeedbackReplied(feedbackId);
//		}
	}
	
	@RequestMapping("/crashStat/list")
	public String crashStatList(CrashStat crashStat,Model model){
		if(crashStat.getStartDate() == null || crashStat.getStartDate().isEmpty()){
			model.addAttribute("list", service.getAllCrashInfo());
		}else{
			model.addAttribute("list", service.getCrashInfoByDate(crashStat));
		}
		model.addAttribute("crashStat", crashStat);
		return "crashStat/list";
	}
}
